package com.wdc.controller;

import com.wdc.constants.Constants;
import com.wdc.entity.dto.TokenUserInfoDto;
import com.wdc.entity.po.UserInfo;
import com.wdc.entity.vo.ResponseVo;
import com.wdc.entity.vo.UserInfoVo;
import com.wdc.exception.BusinessException;
import com.wdc.redis.RedisComponent;
import com.wdc.redis.RedisUtils;
import com.wdc.service.UserInfoService;
import com.wdc.utils.CopyTools;
import com.wf.captcha.ArithmeticCaptcha;
import org.apache.el.parser.Token;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.UUID;

@RequestMapping("/account")
@RestController
@Validated
public class AccountController extends ABaseController {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private RedisComponent redisComponent;

    @RequestMapping("/checkCode")
    public ResponseVo checkCode() {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(100, 43);
        String code = captcha.text();
        String checkCodeBase64 = captcha.toBase64();
        String checkCodeKey = UUID.randomUUID().toString();
        redisUtils.set(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey, code, Constants.REDIS_TIME_1MIN * 10);
        HashMap<String, String> result = new HashMap<>();
        result.put("checkCode", checkCodeBase64);

        return getSuccessResponseVo(result);



    }


    @RequestMapping("/register")
    public ResponseVo register(@NotEmpty String code,
                               @NotEmpty String checkCodeKey,
                               @NotEmpty String password,
                               @NotEmpty @Email String email,
                               @NotEmpty String nickname) {

        try {
            if (!code.equalsIgnoreCase(redisUtils.get(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey))) {
                throw new BusinessException("验证码错误");
            }

            userInfoService.register(password, email, nickname);
            return getSuccessResponseVo(null);
        } finally {
            redisUtils.setRemove(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
        }

    }


    @RequestMapping("/login")
    public ResponseVo login(
            @NotEmpty String checkCodeKey,
            @NotEmpty String password,
            @NotEmpty @Email String email,
            @NotEmpty String checkCode) {

        try {
            if (!checkCode.equalsIgnoreCase(redisUtils.get(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey))) {
                throw new BusinessException("验证码错误");
            }

            UserInfoVo userInfoVo = userInfoService.login(email, password);


            return getSuccessResponseVo(userInfoVo);
        } finally {
//            redisUtils.setRemove(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
        }

    }

    @RequestMapping("/getSysSetting")
    public ResponseVo login() {
        return getSuccessResponseVo(redisComponent.getSysSetting());
    }
}
